<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Db;

class Setting extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['key']) && !empty($post['key'])){
            $where[] = ['key', 'like', '%'.$post['key'].'%'];
        }
        if(isset($post['value']) && !empty($post['value'])){
            $where[] = ['value', 'like', '%'.$post['value'].'%'];
        }
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['desc', 'like', '%'.$post['desc'].'%'];
        }
        $list = Db::name('setting')
            ->order('id desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function setAdd($data)
    {
        $data['key']    = trim($data['key']);
        $data['desc']   = trim($data['desc']);

        $is_encrypt     = $data['is_encrypt'];
        $value          = trim($data['value']);
        if($is_encrypt == 1){
            $data['value'] = base64_encode($value);
        }
        return Db::name('setting')->save($data);
    }

    public function setEdit($data)
    {
        $set_id         = $data['set_id'];
        $is_encrypt     = $data['is_encrypt'] ?? 0;
        $value          = trim($data['value'] ?? '');
        $desc           = trim($data['desc'] ?? '');
        $key            = trim($data['key'] ?? '') ?? "";
        if($key == "contact_us" || $key == "server_desc" || $key == "fund_desc" || $key == "good_user_desc"){
            $update_param   = [
                'desc'      => $desc
            ];
        }else{
            if($is_encrypt == 1){
                $value = base64_encode($value);
            }
            $update_param   = [
                'value'     => $value,
                'is_encrypt'=> $is_encrypt,
                'desc'      => $desc
            ];
        }
        return Db::name('setting')
                ->where(['id'=>$set_id])
                ->update($update_param);
    }

    //取得参数
    public function getValues($skey)
    {
        $info = $this->where(array('key' => $skey))->find();
        if($info){
            if(isjson($info['value'])){
                $info['value'] = json_decode($info['value'],true);
            }
            return $info['value'];
        }else{
            return "";
        }
    }


    /**
     * 一次查询获取多个配置信息
     * @param $skeys
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMultipleValue($skeys)
    {
        $skeys = explode(',', $skeys);
        $newList = [];
        //默认赋值
        foreach($skeys as $v)
        {
            $val = '';
            if(isset($this->skeys[$v]['value']))
            {
                $val = $this->skeys[$v]['value'];
            }
            $newList[$v] = $val;
        }

        //存储赋值
        $where[] = ['skey', 'in', $skeys];
        $list = $this->where($where)->cache(true)->select();
        foreach($list as $v)
        {
            if(isjson($v['value']))
            {
                $v['value'] = json_decode($v['value'],true);
            }
            $newList[$v['skey']] = $v['value'];
        }

        return $newList;
    }
}
